<?php
/*
 * Created on 2008-1-24
 *
 * This is an open source software, please obey the GPL
 * The Referee Service is offered by Cacomand Inc. 
 * View Http://www.cacomand.com for support or more information
 * You can also view distrema.googlecode.com to join the community.
 */
include("lib_user.php");
class ccr_action
//this class is to use to clear the user base
{
	function ccr_action($ccr_host,$ccr_basename,$ccr_power_username,$ccr_power_password)
	{
		if($developer)
			echo("CCR对象成功建立");
		$this->ccr_host=$ccr_host;
		$this->ccr_basename=$ccr_basename;
		$this->ccr_power_username=$ccr_power_username;
		$this->ccr_power_password=$ccr_power_password;
	}
	
	function user_register($username,$password)
	{
		//TODO:判断用户名是否用“_”开头，如果是则返回false
		global $developer;
		$ccr_link=mysql_connect($this->ccr_host,$this->ccr_power_username,$this->ccr_power_password);
			mysql_select_db($this->ccr_basename,$ccr_link);
		if($ccr_link!=FALSE)
		{
			$insert_format='INSERT INTO user_list (username,password) VALUE ("%s","%s")';
			$query=sprintf($insert_format,$username,md5($password));
			if($developer)
				print($query."<br />");
			$res_insert_ID=mysql_query($query,$ccr_link);
			if($user_ID=mysql_insert_ID($ccr_link))
			{
				
				$query_format='CREATE TABLE `variables_%d` (`rec_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,`var_ID` INT UNSIGNED NOT NULL ,`caption` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,`memo` VARCHAR( 500 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci;;';
				$query=sprintf($query_format,$user_ID);
				if($developer)
					print($query."<br />");
				$res_nouse=mysql_query($query,$ccr_link);
				/*
				$query_format="CREATE USER '%s' IDENTIFIED BY '%s';";
				$query=sprintf($query_format,$username,$password);
				if($developer)
				print($query."<br />");
				$res_nouse=mysql_query($query,$ccr_link);
				
				$query_format="GRANT ALL PRIVILEGES ON `user_base`.`variables_%d` TO '%s' IDENTIFIED BY '%s';";
				$query=sprintf($query_format,$user_ID,$username,$password);
				if($developer)
					print($query."<br />");
				$res_nouse=mysql_query($query,$ccr_link);
				
				$query_format="GRANT ALL PRIVILEGES ON `user_base`.`sent_data_pool` TO '%s' IDENTIFIED BY '%s';";
				$query=sprintf($query_format,$username,$password);
				if($developer)
				print($query."<br />");
				$res_nouse=mysql_query($query,$ccr_link);
				*/
				return $user_ID;
			}
			else
			{
				return false;
			}
			mysql_close($ccr_link);
		}
		else
			return false;
	}
	function user_delete()
	{
		global $ccr_host;
		global $developer,$ccr_basename;
		$ccr_link=mysql_connect($ccr_host,$this->ccr_power_username,$this->ccr_power_password);
		mysql_select_db($ccr_basename,$ccr_link);
		if($ccr_link!=FALSE)
		{
			//TODO:删除一个用户的数据表和user_list
		}
	}
}
?>
